﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.Sql;
using System.Data.SqlClient;
using System.Collections;
using GPCSC.Web;
using NickLee.Web.UI;
using NickLee.Common;

namespace GPCSC.Web.SYS_ORG
{
    public partial class userRdit : PageBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                this.f_SelectDataBind();
                this.f_SelectDataBinds();
                this.command.Value = Usual.f_querystringToString("command");
                if (this.command.Value == "View")
                {
                    string postid = Usual.f_querystringToString("CommandArg");
                    f_fristLoad(postid);
                    UIFactory.UIFactory_ReadOnly(this);
                    this.buttontr.Style.Add("display", "none");
                }
                else if (this.command.Value == "Edit")
                {
                    string postid = Usual.f_querystringToString("CommandArg");
                    UIFactory.UIFactory_NotReadOnly(this);
                    f_fristLoad(postid);
                }

            }
        }
        protected void f_fristLoad(string PrimaryKey)
        {
            base.o_uiTable.Clear();
            SqlReadXml org = new SqlReadXml();
            base.o_uiTable.Add("@USERID", PrimaryKey);
            base.m_uiDataSet = org.ReaderDataSet("sel_user_one", base.o_uiTable);
            if (base.m_uiDataSet.Tables.Count > 0)
            {
                if (base.m_uiDataSet.Tables[0].Rows.Count > 0)
                {
                    //UIFactory set UI controls value:界面工厂设置界面控件值
                    try
                    {
                        base.o_uiTable = UIFactory.UIFactory_Set(this, base.m_uiDataSet.Tables[0]);

                    }
                    catch
                    {

                    }
                    //your can do databind yourself:对界面工厂不能填充的数据，通过ODRM手动填充
                    GPCSC.Model.CL_POST_S objModel = new GPCSC.Model.CL_POST_S();
                    var row = base.m_uiDataSet.Tables[0].Rows[0];
                    ODRM.DataRowConvertObject(row, objModel);
                    this.Drop.Text = Usual.f_convertString(row["CL_ROLEname"]);
                    this.ComboBox2.Text = Usual.f_convertString(row["POSTName"]);
                    this.sdg.Value = Usual.f_convertString(row["DepartName"]);
                    this.ddlRegType.Value = Usual.f_convertString(row["PD_CompanyName"]);

                }
            }
        }
        public Hashtable f_getElements(Hashtable o_uiTable)
        {
            //UIFactory Get Values as Hashtable:界面工厂获取值，并保存在哈希表
            base.o_uiTable.Clear();
            string relname = this.MtxtD_REALNAME.Value;
            if (relname == "" || relname == null)
            {
                this.MtxtD_REALNAME.Value = this.MtxtD_USERNAME.Value;

            }
            base.o_uiTable = UIFactory.UIFactory_Get(this);
  
            if (Usual.f_querystringToObj("CommandArg") != null)
            {
                base.o_uiTable.Add("CL_USER_SOID", Usual.f_convertGuid(Usual.f_querystringToObj("CommandArg")));

               
            }
            else
            {
                base.o_uiTable.Add("CL_USER_SOID", Guid.NewGuid());
                
            }
            base.o_uiTable.Add("DepartName", this.MtxtD_DepartName.Text);
            //base.o_uiTable.Add("Contact", this.act.Value);

            return base.o_uiTable;
        }
        private void f_SelectDataBind()
        {
            if (!IsPostBack)
            {
                #region TreeView绑定部门
                try
                {
                    DataSet set1 = new DataSet();
                    SqlReadXml Org = new SqlReadXml();
                    set1 = Org.ReaderDataSet("select_role_all");
                    DataReturn dr = new DataReturn();
                   // ListItemCollection sd = dr.lc(set1, "ROLENAME", "CL_ROLE_SOID");
                    Drop.DataSource = set1;
                    Drop.DataBind();
                  
                }
                catch (System.Exception ee)
                {
                    // base.m_loger.Error("缓存失效或者Session控制错误!", ee);
                    return;
                }
                #endregion
            }
        }
        #region TreeView结构无限级填充



        protected bool buildTree(NickLee.Web.UI.TreeView TreeViewName, DataSet ds)
        {
            //清空所有节点
            TreeViewName.Nodes.Clear();
            //如果为最高级，则修改以适应ds的relation中的关系
            foreach (DataRow dbRow in ds.Tables[0].Rows)
            {
                if (Usual.f_convertString(dbRow["CL_PARMenuID_PK"]) == "00000000-0000-0000-0000-000000000000")
                    //if (dbRow["PARMENUID"].ToString() == "0")
                    //{
                    dbRow["CL_PARMenuID_PK"] = DBNull.Value;
                //}
            }
            //设置ds中父子级relation
            try
            {
                if (ds.Relations["NodeRelation"] == null)
                {
                    ds.Relations.Add("NodeRelation", ds.Tables[0].Columns["CL_Menu_SOID"], ds.Tables[0].Columns["CL_PARMenuID_PK"]);
                }
            }
            catch (Exception e)
            {
                throw new Exception(e.Message);
            }
            //循环绑定父node
            foreach (DataRow dbRow in ds.Tables[0].Rows)
            {
                if (dbRow.IsNull("CL_PARMenuID_PK"))
                {
                    TreeViewNode newNode =
                        CreateTreeNode(dbRow["MenuName"].ToString(), dbRow["CL_Menu_SOID"].ToString() + "&" + dbRow["DEPTH"].ToString());
                    if (newNode != null)
                    {
                        TreeViewName.Nodes.Add(newNode);
                    }
                    PopulateSubTree(dbRow, newNode);
                }
            }
            return true;
        }

        /// <summary>
        /// 循环绑定子node
        /// </summary>
        /// <param name="dbRow">dataset的行</param>
        /// <param name="node">需要添加的节点</param>
        private void PopulateSubTree(DataRow dbRow, TreeViewNode node)
        {
            foreach (DataRow childRow in dbRow.GetChildRows("NodeRelation"))
            {
                TreeViewNode childNode = CreateTreeNode(childRow["POSTNAME"].ToString(), childRow["CL_POST_SOID"].ToString() + "&" + childRow["DEPTH"].ToString());
                if (childNode != null)
                {
                    node.Nodes.Add(childNode);
                }
                PopulateSubTree(childRow, childNode);
            }
        }


        /// <summary>
        /// 给所有node附加属性
        /// </summary>
        /// <param name="text">呈现文本</param>
        /// <returns>返回node</returns>
        private TreeViewNode CreateTreeNode(string text, string id)
        {
            TreeViewNode node = new TreeViewNode();
            node.Text = text;
            node.ID = id;
            node.Expanded = true;
            // node.ContentCallbackUrl = "./N_SEC_USER_ListTreeViewXml.aspx?ParentNodeId=" + id.ToString();
            return node;
        }
        #endregion


        private void f_SelectDataBinds()
        {
            if (!IsPostBack)
            {
                #region TreeView绑定部门
                try
                {
                    DataSet set1 = new DataSet();
                    SqlReadXml Org = new SqlReadXml();
                    set1 = Org.ReaderDataSet("sel_POST_all");
                    this.buildTrees(TreeView2, set1);

                }
                catch (System.Exception ee)
                {
                    // base.m_loger.Error("缓存失效或者Session控制错误!", ee);
                    return;
                }
                #endregion
            }
        }
        #region TreeView结构无限级填充



        protected bool buildTrees(NickLee.Web.UI.TreeView TreeViewName, DataSet ds)
        {
            //清空所有节点
            TreeViewName.Nodes.Clear();
            //如果为最高级，则修改以适应ds的relation中的关系
            foreach (DataRow dbRow in ds.Tables[0].Rows)
            {
                if (Usual.f_convertString(dbRow["CL_PARPOSTID_PK"]) == "00000000-0000-0000-0000-000000000000")
                    //if (dbRow["PARMENUID"].ToString() == "0")
                    //{
                    dbRow["CL_PARPOSTID_PK"] = DBNull.Value;
                //}
            }
            //设置ds中父子级relation
            try
            {
                if (ds.Relations["NodeRelation"] == null)
                {
                    ds.Relations.Add("NodeRelation", ds.Tables[0].Columns["CL_POST_SOID"], ds.Tables[0].Columns["CL_PARPOSTID_PK"]);
                }
            }
            catch (Exception e)
            {
                throw new Exception(e.Message);
            }
            //循环绑定父node
            foreach (DataRow dbRow in ds.Tables[0].Rows)
            {
                if (dbRow.IsNull("CL_PARPOSTID_PK"))
                {
                    TreeViewNode newNode =
                        CreateTreeNodes(dbRow["POSTNAME"].ToString(), dbRow["CL_POST_SOID"].ToString() + "&" + dbRow["DEPTH"].ToString());
                    if (newNode != null)
                    {
                        TreeViewName.Nodes.Add(newNode);
                    }
                    PopulateSubTrees(dbRow, newNode);
                }
            }
            return true;
        }

        /// <summary>
        /// 循环绑定子node
        /// </summary>
        /// <param name="dbRow">dataset的行</param>
        /// <param name="node">需要添加的节点</param>
        private void PopulateSubTrees(DataRow dbRow, TreeViewNode node)
        {
            foreach (DataRow childRow in dbRow.GetChildRows("NodeRelation"))
            {
                TreeViewNode childNode = CreateTreeNodes(childRow["POSTNAME"].ToString(), childRow["CL_POST_SOID"].ToString() + "&" + childRow["DEPTH"].ToString());
                if (childNode != null)
                {
                    node.Nodes.Add(childNode);
                }
                PopulateSubTree(childRow, childNode);
            }
        }


        /// <summary>
        /// 给所有node附加属性
        /// </summary>
        /// <param name="text">呈现文本</param>
        /// <returns>返回node</returns>
        private TreeViewNode CreateTreeNodes(string text, string id)
        {
            TreeViewNode node = new TreeViewNode();
            node.Text = text;
            node.ID = id;
            node.Expanded = true;
            // node.ContentCallbackUrl = "./N_SEC_USER_ListTreeViewXml.aspx?ParentNodeId=" + id.ToString();
            return node;
        }
        #endregion

        protected void Button1_Click(object sender, EventArgs e)
        {
            if (MtxtD_CL_ROLEOID_PK.Text == "")
            {
                MtxtD_CL_ROLEOID_PK.Text = "00000000-0000-0000-0000-000000000000";
            }
            if (MtxtD_CL_POSTID_PK.Text == "")
            {
                MtxtD_CL_POSTID_PK.Text = "00000000-0000-0000-0000-000000000000";
            }
            savepost();
        }
        protected void savepost()
        {
            try
            {
                Hashtable htable = f_getElements(base.o_uiTable);

                SQLServerDAL.databyhtable dtable = new SQLServerDAL.databyhtable();
                if (this.command.Value == "new")
                {
                    dtable.databyhtables(htable, "add", "CL_USER_S", "", "");
                    this.Response.Write("<script>alert('添加人员成功！');window.location.href='userList.aspx';</script>");
                }
                else if (this.command.Value == "Edit")
                {
                    dtable.databyhtables(htable, "update", "CL_USER_S", "CL_USER_SOID", Usual.f_querystringToObj("CommandArg").ToString());
                    this.Response.Write("<script>alert('更新人员成功！');window.location.href='userList.aspx';</script>");
                }
            }
            catch (Exception ee)
            {
                throw ee;
            }


        }
        
    }
}